﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls.WebParts;

namespace Camelot.JSONAPI.Extras.Examples.Webparts.SharePointJsonBlogReader
{
    [ToolboxItemAttribute(false)]
    public partial class SharePointJsonBlogReader : WebPart
    {

        #region Property Blog feed settings

        //public enum SelectVersionEnum { Version2010, Version2007 };
        //protected SelectVersionEnum selectVersion;
        //[Category("SharePoint Blog Settings"),
        //WebBrowsable(true),
        //Personalizable(PersonalizationScope.Shared),
        //WebDisplayName("SharePoint Version"),
        //WebDescription("SharePoint version where the blog is kept. Version2010 means SharePoint 2010 or newer, Version2007 means SharePoint 2007 or older.")]
        //public SelectVersionEnum SelectVersion
        //{
        //    get { return selectVersion; }
        //    set { selectVersion = value; }
        //}

        private string blogLocation;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Blog web"),
        WebDescription("The url to the blog, ie. sites/blog")]
        public string BlogLocation
        {
            get { return blogLocation; }
            set { blogLocation = value; }
        }


        private string blogCategory;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Category/Categories"),
        WebDescription("Type category/categories to fetch, separate with semicolon.")]
        public string BlogCategory
        {
            get { return blogCategory; }
            set { blogCategory = value; }
        }

        private int posts;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("No. posts"),
        WebDescription("How many posts to fetch")]
        public int Posts
        {
            get
            {
                if (posts != 0)
                    return posts;
                else
                    return 5; // Let's have 5 as default!
            }
            set { posts = value; }
        }

        public enum TitlesByEnum { FALSE, TRUE }; // Set default value first!
        protected TitlesByEnum titlesOnly;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Show titles only"),
        WebDescription("If TRUE the control only renders the title link.")]
        public TitlesByEnum TitlesOnly
        {
            get { return titlesOnly; }
            set { titlesOnly = value; }
        }

        public enum SortByEnum { DESC, ASC }; // Set default value first!
        protected SortByEnum sortOrder;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Sort order"),
        WebDescription("ASC = Oldest post first, DESC = Newest post first")]
        public SortByEnum SortOrder
        {
            get { return sortOrder; }
            set { sortOrder = value; }
        }

        private int wordCount;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("No. words"),
        WebDescription("How many words to display, 0 will render the entire post.")]
        public int WordCount
        {
            get
            {
                return wordCount;
            }
            set { wordCount = value; }
        }

        public enum ShowByEnum { TRUE, FALSE }; // Set default value first!
        protected ShowByEnum showReadMore;
        [Category("SharePoint Blog Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Show read more link"),
        WebDescription("If TRUE the control renders a read more link.")]
        public ShowByEnum ShowReadMore
        {
            get { return showReadMore; }
            set { showReadMore = value; }
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Pass the settings to a JSON object
            var settings = new BlogReaderSettings()
            {
                BlogLocation = BlogLocation,
                BlogCategory = BlogCategory,
                Posts = Posts.ToString(CultureInfo.InvariantCulture),
                ShowReadMore = ShowReadMore.ToString(),
                SortOrder = SortOrder.ToString(),
                TitlesOnly = TitlesOnly.ToString(),
                WordCount = WordCount.ToString(CultureInfo.InvariantCulture)
            };

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(settings);
            string script = string.Format(@"<script type=""text/javascript"">var settings={0}</script>", json);

            if(!Page.ClientScript.IsClientScriptBlockRegistered("blogClientScript"))
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "blogClientScript", script, false);
            }
        }
    }
}
